package keeto.com.vn.bms.ui.adapter;

import hirondelle.date4j.DateTime;

import java.util.ArrayList;
import java.util.Date;

import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.utils.Utils;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	private ArrayList<DateTime> arrDate;
	private int month;
	private int year;
	private Context context;
	private int startDayofWeek = 2;
	protected DateTime today;
	public ArrayList<DateTime> getArrDate() {
		return arrDate;
	}
	public void setArrDate(ArrayList<DateTime> arrDate) {
		this.arrDate = arrDate;
	}
	public CalendarAdapter(int month, int year, Context context) {
		super();
		this.month = month;
		this.year = year;
		this.context = context;
		this.arrDate = Utils.getFullWeeks(month, year, startDayofWeek);
	}

	public void setAdapterCalendar(DateTime dateTime){
		this.month = dateTime.getMonth();
		this.year = dateTime.getYear();
		this.arrDate = Utils.getFullWeeks(month, year, startDayofWeek);

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrDate.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	public void updateToday() {
		today = Utils.convertDateToDateTime(new Date());		
	}

	protected DateTime getToday() {
		if (today == null) {
			today = Utils.convertDateToDateTime(new Date());
		}
		return today;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TextView tvDate;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_calendar, null);
		}
		tvDate = (TextView)convertView.findViewById(R.id.tv_date_cal);

		DateTime time = arrDate.get(position);
		if (time != null){
			if(time.getWeekDay() == 7 || time.getWeekDay() == 1){
				tvDate.setTextColor(context.getResources().getColor(R.color.tv_color));
			}else{
				tvDate.setTextColor(context.getResources().getColor(R.color.black_white_selector));
			}
			if (time.equals(getToday())){
				tvDate.setBackgroundColor(context.getResources().getColor(R.color.yellow));
				tvDate.setTextColor(Color.WHITE);
				tvDate.setText(time.getDay() + "");
			}else{
				tvDate.setText(time.getDay() + "");
			}
		}else{
			tvDate.setText("");
		}
		return convertView;
	}



}